﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Drawing;
using Microsoft.Win32;

namespace BingWallpaper
{
    public partial class Service1 : ServiceBase
    {
        System.Threading.Timer BingTimer;
        private static String imgDir = @"C:\Windows\Web\Wallpaper\Bing\";
        [DllImport("user32.dll")]
        private static extern bool SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
        public static void SetWallpaper(string path)
        {
            SystemParametersInfo(20, 0, path, 0x01 | 0x02);
        }
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            GetBingWallpaper();  
        }

        protected override void OnStop()
        {
            if (BingTimer != null)  
           {
               BingTimer.Dispose();  
           }  
        }

       private void GetBingWallpaper()  
       {  
           TimerCallback timerCallback = new TimerCallback(CallbackTask);  
  
  
           AutoResetEvent autoEvent = new AutoResetEvent(false);

           
           BingTimer = new System.Threading.Timer(timerCallback, autoEvent, 10000, 60000 * 10);  
       }  
  
  
       private void CallbackTask(Object stateInfo)  
       {
           WallpaperSet.Set(@"C:\Windows\Web\Wallpaper\Bing\2017-02-26.jpg", WallpaperSet.Style.Centered);
           //SetWallpaper(@"C:\Windows\Web\Wallpaper\Bing\2017-02-26.jpg");
           String imageFileName;

           if (!Directory.Exists(imgDir))
               Directory.CreateDirectory(imgDir);

           
               imageFileName = imgDir + DateTime.Today.ToString("yyy-MM-dd") + ".jpg";
               if (!File.Exists(imageFileName))
               {
                   string response = null;
                   Connect(ref response, 0);
                   ProcessXml(ref response);
                   using (WebClient client = new WebClient())
                       client.DownloadFile("http://www.bing.com" + response + "_1920x1080.jpg", imageFileName);
                   
               }
               

       }

       private static void Connect(ref string res, byte i)
       {
           HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create("http://www.bing.com/hpimagearchive.aspx?format=xml&idx=" + i + "&n=1&mbl=1");
           webrequest.KeepAlive = false;
           webrequest.Method = "GET";
           using (HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse())
           using (StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream()))
               res = loResponseStream.ReadToEnd();
       }

       private static void ProcessXml(ref string xmlString)
       {
           using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(new StringReader(xmlString)))
           {
               reader.ReadToFollowing("urlBase");
               xmlString = reader.ReadElementContentAsString();
           }
       }

    }
}
